from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import time
# 启动浏览器
driver=webdriver.Edge()
# 窗口最大化
driver.maximize_window()
driver.get('https://www.baidu.com/Index.htm')
# 点击贴吧 显示等待
a=WebDriverWait(driver,10,0.5).until(EC.presence_of_element_located((By.XPATH,'//a[@href="http://tieba.baidu.com/"]')))
a.click()
# 切换页面
driver.switch_to.window(driver.window_handles[-1])
# 搜索内容
a=driver.find_element(By.XPATH,'//*[@id="wd1"]')
a.send_keys('孙笑川')
# 键盘事件 回车
a.send_keys(Keys.ENTER)
time.sleep(3)
driver.find_element(By.XPATH,'//*[@id="thread_top_list"]/li[1]/div/div[2]/div/div[1]/a').click()
time.sleep(3)